﻿using Dao;
using Model;
using System;
using System.Collections.Generic;
using System.Text;

public partial class Web_Back_BookList : System.Web.UI.Page
{

    //用来生成用户列表的html标签
    public string BooksList { get; set; }
    private StateDao StateDao=new StateDao();

    protected void Page_Load(object sender, EventArgs e)
    {
        //在Session中取admin对象
        Account admin = (Account)Session["admin"];
        //判断是否已经登录
        if (admin == null)
        {
            Response.Redirect("../Login.aspx");
        }
        else
        {
            BookState.Text = "1";
            //刷新列表
            UpdateList();
        }
    }

    private void UpdateList()
    {
        //实例化一个bookDao
        //Dao数据库访问操作
        BookDao bookDao = new BookDao();
        //通过bookDao对象取出所有的书的信息
        List<BookInfo> list = bookDao.GetAllBooks();
        RefreshTable(list);
    }

    // 添加图书
    protected void BtnAddBook_Click(object sender, EventArgs e)
    {
        BookInfo book = new BookInfo();
        book.Author = TextBookAuthor.Text;
        book.Translator = TextTranslator.Text;
        book.Pubname = TextPubname.Text;
        book.Oneword = TextOneword.Text;
        book.Description = TextBookDescription.Text;
        book.Price = Convert.ToDouble(TextBookPrice.Text);
        book.Image = TextBookImage.Text;
        book.Name = TextBookName.Text;
        book.TypeId = Convert.ToInt32(DropDownList1.SelectedValue);
        book.State = Convert.ToInt32(DropDownList2.SelectedValue);
        //实例化一个bookDao
        //Dao数据库访问操作
        BookDao bookDao = new BookDao();
        if (bookDao.AddNewBook(book))
        {
            Response.AddHeader("Refresh", "0");
        }
    }

    // 删除图书
    protected void BtnDeleteBook_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(BookId.Text);
        //实例化一个bookDao
        BookDao bookDao = new BookDao();
        //调用删除方法删除图书
        if (bookDao.DeleteBookById(id))
        {
            //刷新当前页 0为立即刷新
            Response.AddHeader("Refresh", "0");
        }
    }

    //获取上架书籍
    protected void BtnGetUp_Click(object sender, EventArgs e)
    {
        int state = 1;
        BookState.Text = "1";
        //实例化一个bookDao
        BookDao bookDao = new BookDao();
        //调用通过状态查找图书方法
        List<BookInfo> list = bookDao.GetBookByState(state);
        RefreshTable(list);
    }

    //获取待上架书籍
    protected void BtnGetWaitUp_Click(object sender, EventArgs e)
    {
        int state = 2;
        BookState.Text = "2";
        //实例化一个bookDao
        BookDao bookDao = new BookDao();
        //调用通过状态查找图书方法
        List<BookInfo> list = bookDao.GetBookByState(state);
        RefreshTable(list);
    }

    //获取下架书籍
    protected void BtnDown_Click(object sender, EventArgs e)
    {
        int state = 3;
        BookState.Text = "3";
        //实例化一个bookDao
        BookDao bookDao = new BookDao();
        //调用通过状态查找图书方法
        List<BookInfo> list = bookDao.GetBookByState(state);
        RefreshTable(list);
    }

    protected void RefreshTable(List<BookInfo> list)
    {

        StringBuilder sb = new StringBuilder();
        if (list != null)
        {
            //遍历列表
            foreach (BookInfo book in list)
            {

                if (book.Description.Length > 65)
                {
                    book.Description = book.Description.Substring(0,65)+"...";
                }

                   //生成html
                   sb.AppendFormat("<tr class='odd gradeX'>" +
                    "<td>{10}</td><td style='width:10%;'><img width='100%' src='../{0}'/></td>" +
                    "<td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td class='state'>{9}</td>" +
                    "<td>" +
                    "<div bookId = '{10}' class='btn-group-sm text-center'>" +
                    "<button type = 'button' class='btn btn-success btn_update'>编辑</button><p> </p>" +
                    "<button type = 'button' class='btn btn-danger btn_delete' data-toggle='modal' data-target='#confirm-delete'>删除</button></div>" +
                    "</td></tr>", book.Image, book.Name, book.Author,book.Translator,book.Pubname,book.Oneword, book.Description,  book.Price, book.TypeName,StateDao.GetBookStateById(book.State), book.Id);
            }
        }
        else
        {
            sb.AppendFormat("<tr class ='text - center'><td colspan='12' class ='text-center'>还没有相关书籍信息哦！</td><tr>");
        }
        //返回给页面
        BooksList = sb.ToString();
    }
}